#include "p33Fxxxx.h"
#include "FIRFilter.h"
#include "dsp.h"


/****** CONFIGURAR OSCILADOR ***********************************************/
// External Oscillator
_FOSCSEL(FNOSC_FRC);					
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_NONE);	// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
													// OSC2 Pin Function: OSC2 is Clock Output
													// Primary Oscillator Mode: Disabled

_FWDT(FWDTEN_OFF); 						            // Watchdog Timer Enabled/disabled by user software
													// (LPRC can be disabled by clearing SWDTEN bit in RCON register
_FPOR(FPWRT_PWR1);  					 			// Turn off the power-up timers.
_FGS(GCP_OFF);            							// Disable Code Protection


/****** DEFINIR CONSTANTES ***************************************************/

#define BLOCK_LENGTH    256          				/*We will filter a block of 1024 samples in this example*/
#define LOWPASSFILTER           					/*<---Comment out this line of the code if BPF is desired */

/****** DEFINIR VARIABLES ****************************************************/

int i;

/****** IMPORTAR ARCHIVOS .s GENERADOS POR dsPICWorks Y FilterDesing *********/

extern fractional square[256];        			/* Input Array containing Square wave signal */
                                        		/* of frequency 80 Hz sampled at 11000 Hz*/

extern FIRStruct lowpassexampleFilter;  		/*Contains filter structures for FIR-LPF*/


/****** VECTORES DE OPERACION DE FILTRO  *************************************/

fractional temp[1] ;    	
fractional Muestratemp[1] ;  
fractional salida[BLOCK_LENGTH];   /*Output array where filtered output will be stored */

int main(void) {

// Configure Oscillator to operate the device at 39.6 Mhz
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 7.37 M*43/(2*2)=79.22 Mhz for 7.37 MHz input clock
	PLLFBD=41;					// M=43
	CLKDIVbits.PLLPOST=0;		// N1=2
	CLKDIVbits.PLLPRE=0;		// N2=2
	OSCTUN=0;					// Tune FRC oscillator, if FRC is used


// Disable Watch Dog Timer
	RCONbits.SWDTEN=0;


// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x01);		// Initiate Clock Switch to
										// FRC with PLL (NOSC=0b001)
	__builtin_write_OSCCONL(0x01);		// Start clock switching

	while (OSCCONbits.COSC != 0b001);	// Wait for Clock switch to occur	


// Wait for PLL to lock
	while(OSCCONbits.LOCK!=0) {};

        /* FILTER OPERATIONS */
		  	configurarfilterinit();
			FIRFilterInit();
			for (i = 0;i<257;i++){
				Muestratemp[0]=square[i];
				configurarblock();				
				BlockFIRFilter();
				salida[i]= temp[0];
			}
			    
    while (1);
}
